package com.mlethe.library.recyclerview.viewholder;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

/**
 * 方便 DataBinding 的使用
 *
 * @author Mlethe
 * @date 2020/9/15
 */
public class DataBindingViewHolder extends ViewHolder {

    private ViewDataBinding binding;

    public DataBindingViewHolder(@NonNull View itemView) {
        super(itemView);
        binding = DataBindingUtil.bind(itemView);
    }

    /**
     * 绑定数据
     *
     * @param variableId 要设置的变量的BR id。例如，如果变量是x，那么variableId将是BR.x。
     * @param object The new value of the variable to be set.
     * @return DataBindingViewHolder
     */
    public DataBindingViewHolder setVariable(int variableId, Object object) {
        binding.setVariable(variableId, object);
        return this;
    }

    /**
     * 此方法必须执行在UI线程，当绑定的数据修改时更新视图
     *
     * @return DataBindingViewHolder
     */
    public DataBindingViewHolder executePendingBindings() {
        binding.executePendingBindings();
        return this;
    }
}
